<template>
  <page-header-wrapper :title="false">
    <a-card title="押金详情">
      <!-- <template #extra> <a-button type="primary" @click="handleEdit(detail)">编辑账户</a-button></template> -->
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="订单ID" :span="3"> {{ detail.id }} </a-descriptions-item>
        <a-descriptions-item label="创建人" :span="3"> {{ detail.creatorName }} </a-descriptions-item>
        <a-descriptions-item label="日期" :span="3"> {{ detail.createTime }}</a-descriptions-item>
        <a-descriptions-item label="金额" :span="3"> {{ detail.amount ?? 0 }}元 </a-descriptions-item>
        <a-descriptions-item label="归属店" :span="3"> {{ detail.storeName }} </a-descriptions-item>
        <!-- <a-descriptions-item label="分摊月数" :span="3"> {{ detail.id }} </a-descriptions-item> -->
        <a-descriptions-item label="账户" :span="3"> {{ detail.accountName }} </a-descriptions-item>
        <a-descriptions-item label="状态" :span="3">
          <template>
            <a-tag color="#82af6f" v-if="detail?.status == 1">已退还</a-tag>
            <a-tag color="#D15B47" v-else>待归还</a-tag>
          </template>
        </a-descriptions-item>
        <a-descriptions-item label="审核状态" :span="3">
          <template>
            <a-tag color="#82af6f" v-if="detail?.checkStatus == 1">已审核</a-tag>
            <a-tag color="#D15B47" v-else>待审核</a-tag>
          </template>
        </a-descriptions-item>
        <a-descriptions-item label="审核人" :span="3" v-if="detail?.checkStatus == 1"> {{ detail.checkerName }} </a-descriptions-item>
        <a-descriptions-item label="备注" :span="3"> {{ detail.memo }} </a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex">
            <a-tag color="#428bca" @click="handleRecordCreate()" v-if="detail?.status != 1">归还</a-tag>
            <a-tag color="#428bca" @click="handleEdit(detail)" v-if="detail?.checkStatus == 0">编辑</a-tag>
            <a-tag color="#82AF6F" @click="setSH()" v-if="detail?.checkStatus == 0">审核</a-tag>
            <a-tag color="#82AF6F" @click="setFSH()" v-else>反审核</a-tag>
            <a-tag color="#D15B47" @click="setCX()" v-if="detail?.checkStatus == 0">撤销</a-tag>
          </div>
        </a-descriptions-item>
      </a-descriptions>
      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
    <a-card title="操作记录" style="margin-top: 24px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(queryParam)" :loading="recordLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <template> {{ record.empName }} <a-tag color="#428bca" @click="handleUserDetail(record)">详情</a-tag> </template>
          </template>
        </span>
      </a-table>
    </a-card>
    <record-form
      ref="recordModal"
      :visible="recordVisible"
      :loading="recordConfirmLoading"
      :model="recordMdl"
      @cancel="handleRecordCancel"
      @ok="handleRecordOk"
    />
  </page-header-wrapper>
</template>

<script>
import { getOperRecordListUsingPOST } from '@/api/api/caigouguanli.ts'
import CreateForm from './CreateForm'
import RecordForm from './RecordForm'
import {depositCheckPurUsingPOST,repayUsingPOST,depositUnCheckPurUsingPOST, getDepositUsingGET,revokeDepositUsingPOST, updateDepositUsingPOST } from '@/api/api/qiantaizhangwu.ts'
export default {
  name: 'CashierDepositDetail',
  components: {
    CreateForm,
    RecordForm,
  },
  data() {
    return {
      mdl: null,
      visible: false,
      confirmLoading: false,
      recordMdl: null,
      recordVisible: false,
      recordConfirmLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      pagination: {},
      advanced: false,
      detail: {},
      recordLoading: false,
      columns: [
        {
          title: '日期',
          dataIndex: 'operTime',
        },
        {
          title: '类型',
          dataIndex: 'action',
        },
        {
          title: '操作者',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loadData: [],
    }
  },
  mounted() {
    this.getDetail()
    this.handleSearch(this.queryParam)
  },
  methods: {
    setSH() {
      const that = this
      that.$confirm({
        title: '审核订单',
        content: '你确定要审核通过该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          depositCheckPurUsingPOST({ id: that.detail.id })
            .then(() => {
              that.getDetail()
              that.handleSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setFSH() {
      const that = this
      that.$confirm({
        title: '反审核',
        content: '你确定要反审核该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          depositUnCheckPurUsingPOST({ id: that.detail.id })
            .then(() => {
              that.getDetail()
              that.handleSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setCX() {
      const that = this
      that.$confirm({
        title: '撤销订单',
        content: '你确定要撤销该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          revokeDepositUsingPOST({ id: that.detail.id })
            .then(() => {
              that.$router.back()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    getDetail() {
      getDepositUsingGET({ id: this.$route.params.id })
        .then((res) => {
          this.detail = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleEdit(record) {
      this.mdl = { ...record }
      this.visible = true
    },
    handleOk() {
      this.confirmLoading = true
      const form = this.$refs.createModal.form
      form.validateFields((errors, values) => {
        if (!errors) {
          updateDepositUsingPOST({ ...values })
            .then(() => {
              this.getDetail()
              this.handleSearch(this.queryParam)
              this.confirmLoading = false
              this.visible = false
              this.$message.success('修改成功')
            })
            .catch(() => {
              this.confirmLoading = false
              this.$message.error('修改失败,请联系管理员')
            })
        } else {
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSearch(params) {
      this.recordLoading = true
      getOperRecordListUsingPOST({ ...params, orderId: this.$route.params.id })
        .then((res) => {
          this.loadData = res.data.list
          this.recordLoading = false
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleRecordTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.recordPagination = pager
      this.handleRecordSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: '',
        status: '',
      })
    },
    handleUserDetail(record) {
      this.$router.push({ path: `/user/user/${record.operator}` })
    },
    handleRecordCreate() {
      this.recordMdl = {}
      this.recordVisible = true
    },
    handleRecordOk() {
      this.recordConfirmLoading = true
      const form1 = this.$refs.recordModal.form
      form1.validateFields((errors, values) => {
        if (!errors) {
          repayUsingPOST({ ...values,orderId: this.$route.params.id ,retTime:values.retTime })
            .then(() => {
              this.getDetail()
              this.handleSearch(this.queryParam)
              this.recordVisible = false
              this.recordConfirmLoading = false
              this.$message.success('修改成功')
            })
            .catch(() => {
              this.recordConfirmLoading = false
              this.$message.error('修改失败,请联系管理员')
            })
        } else {
          this.recordConfirmLoading = false
        }
      })
    },
    handleRecordCancel() {
      this.recordVisible = false
      const form = this.$refs.recordModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
  },
}
</script>
